package cn.touna.jss.modules.custom.carmodel.service;

import cn.touna.jss.common.factory.ResponseFactory;
import cn.touna.jss.common.model.Page;
import cn.touna.jss.common.model.PageResult;
import cn.touna.jss.common.util.ValidUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.custom.carmodel.dto.*;
import cn.touna.jss.modules.custom.carmodel.entity.*;
import cn.touna.jss.modules.custom.carmodel.mapper.*;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车型匹配服务
 * Created by Steve.Tao on 2018/2/7.
 */
@Service
public class CarModelService {

    @Autowired
    private Car300BrandEntityMapper car300BrandEntityMapper;

    @Autowired
    private JzgBrandEntityMapper jzgBrandEntityMapper;

    @Autowired
    private Car300SeriesEntityMapper car300SeriesEntityMapper;

    @Autowired
    private JzgSeriesEntityMapper jzgSeriesEntityMapper;

    @Autowired
    private Car300ModelEntityMapper car300ModelEntityMapper;

    @Autowired
    private JzgModelEntityMapper jzgModelEntityMapper;

    @Autowired
    private ModelMapEntityMapper modelMapEntityMapper;

    /**
     * 获取车300品牌
     *
     * @param page
     * @param request
     * @return
     */
    @ApiMapping(value = "carmodel.getCar300BrandList",recordHistory = false)
    public Response getCar300BrandList(Page page, BrandListRequest request) {
        String brandName = request.getBrandName();
        List<Car300BrandEntity> list = car300BrandEntityMapper.selectByPage(page.getPageSize(), page.getOffset(), brandName);
        int total = car300BrandEntityMapper.selectCount(brandName);
        return ResponseFactory.success(PageResult.build(page, total, list));
    }

    /**
     * 获取车300车系
     *
     * @param page
     * @param request
     * @return
     */
    @ApiMapping(value = "carmodel.getCar300SeriesList",recordHistory = false)
    public Response getCar300SeriesList(Page page, SeriesListRequest request) {
        Response response = ValidUtil.valid(request);
        if (response != null)
            return response;

        String seriesName = request.getSeriesName();
        int brandId = request.getBrandId();
        List<Car300SeriesEntity> list = car300SeriesEntityMapper.selectByPage(brandId, page.getPageSize(), page.getOffset(), seriesName);
        int total = car300SeriesEntityMapper.selectCount(brandId, seriesName);
        return ResponseFactory.success(PageResult.build(page, total, list));
    }

    /**
     * 获取车300车型列表
     *
     * @param page
     * @param request
     * @return
     */
    @ApiMapping("carmodel.getCar300ModelList")
    public Response getCar300ModelList(Page page, ModelListRequest request) {
        Response response = ValidUtil.valid(request);
        if (response != null)
            return response;

        List<Car300ModelDto> list = car300ModelEntityMapper.selectByPageForMatch(
                request.getBrandId(),
                request.getSeriesId(),
                request.getMatch(),
                page.getPageSize(),
                page.getOffset());
        int total = car300ModelEntityMapper.selectCountForMatch(
                request.getBrandId(),
                request.getSeriesId(),
                request.getMatch()
        );
        return ResponseFactory.success(PageResult.build(page, total, list));
    }

    /**
     * 获取精真估品牌列表
     *
     * @param page
     * @param request
     * @return
     */
    @ApiMapping("carmodel.getJzgBrandList")
    public Response getJzgBrandList(Page page, BrandListRequest request) {
        String brandName = request.getBrandName();
        List<JzgBrandEntity> list = jzgBrandEntityMapper.selectByPage(page.getPageSize(), page.getOffset(), brandName);
        int total = jzgBrandEntityMapper.selectCount(brandName);
        return ResponseFactory.success(PageResult.build(page, total, list));
    }

    /**
     * 获取精真估车系
     *
     * @param page
     * @param request
     * @return
     */
    @ApiMapping("carmodel.getJzgSeriesList")
    public Response getJzgSeriesList(Page page, SeriesListRequest request) {
        Response response = ValidUtil.valid(request);
        if (response != null)
            return response;

        String seriesName = request.getSeriesName();
        int brandId = request.getBrandId();
        List<JzgSeriesEntity> list = jzgSeriesEntityMapper.selectByPage(brandId, page.getPageSize(), page.getOffset(), seriesName);
        int total = jzgSeriesEntityMapper.selectCount(brandId, seriesName);
        return ResponseFactory.success(PageResult.build(page, total, list));
    }

    /**
     * 获取精真估车型列表
     *
     * @param page
     * @param request
     * @return
     */
    @ApiMapping("carmodel.getJzgModelList")
    public Response getJzgModelList(Page page, ModelListRequest request) {
        Response response = ValidUtil.valid(request);
        if (response != null)
            return response;

        List<JzgModelDto> list = jzgModelEntityMapper.selectByPageForMatch(
                request.getBrandId(),
                request.getSeriesId(),
                request.getModelName(),
                page.getPageSize(),
                page.getOffset()
        );
        int total = jzgModelEntityMapper.selectCountForMatch(
                request.getBrandId(),
                request.getSeriesId(),
                request.getModelName());
        return ResponseFactory.success(PageResult.build(page, total, list));
    }

    /**
     * 修改车型匹配
     *
     * @param request
     * @return
     */
    @ApiMapping("carmodel.saveModelIdRef")
    public Response saveModelIdRef(ModelMapEntity request) {
        Response response = ValidUtil.valid(request);
        if (response != null)
            return response;

        ModelMapEntityExample example1 = new ModelMapEntityExample();
        example1.createCriteria()
                .andCar300ModelIdEqualTo(request.getCar300ModelId())
                .andJzgModelIdEqualTo(request.getJzgModelId());

        ModelMapEntity entity1 = getByExample(example1);

        if (entity1 == null) {  //不存在该车型对
            ModelMapEntityExample example2 = new ModelMapEntityExample();
            example2.createCriteria()
                    .andCar300ModelIdEqualTo(request.getJzgModelId());
            ModelMapEntity entity2 = getByExample(example2);
            if (entity2 == null) {  //精真估的车型也没有已经被匹配的项
                ModelMapEntityExample example3 = new ModelMapEntityExample();
                example2.createCriteria()
                        .andCar300ModelIdEqualTo(request.getCar300ModelId());
                ModelMapEntity entity3 = getByExample(example3);
                if (entity3 == null) {
                    modelMapEntityMapper.insertModelIdRef(request);
                } else {
                    modelMapEntityMapper.updateModelIdRef(request);
                }
                return ResponseFactory.success(null);
            } else {
                JzgModelEntity jzgModelEntity = jzgModelEntityMapper.selectByModelId(request.getJzgModelId());
                if (jzgModelEntity == null) {
                    return ResponseFactory.fail(SystemErrorCodeEnum.CRS_EDIT_FAILED, "不存在精真估车型");
                }
                Car300ModelEntity car300ModelEntity = car300ModelEntityMapper.selectByModelId(request.getCar300ModelId());
                return ResponseFactory.fail(SystemErrorCodeEnum.CRS_EDIT_FAILED, "需要解除匹配：" + car300ModelEntity.getModelName() + "(车300)-"
                        + jzgModelEntity.getModelName() + "(精真估)才能执行当前的匹配");
            }
        } else {
            return ResponseFactory.fail(SystemErrorCodeEnum.CRS_EDIT_FAILED, "已经存在匹配项");
        }
    }

    private ModelMapEntity getByExample(ModelMapEntityExample example) {
        List<ModelMapEntity> list = modelMapEntityMapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }
}
